package com.hualan.service;

import com.alibaba.fastjson.JSON;
import com.hualan.entity.Users;
import com.hualan.utils.EntityFactory;
import com.hualan.utils.ExecUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = {"/login", "/register", "/users", "/zhuanzhang"})
public class UserService extends HttpServlet {
    private List<Users> usersList;

    @Override
    public void init() throws ServletException {
        this.usersList = new ArrayList<>();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取所有的用户信息
        this.usersList.clear();
        //获取请求网址
        if(!req.getServletPath().equals("/users")){
            resp.getWriter().write("403 not allowed");
            return;
        }
        //定义sql语句完成数据库指定的操作
        //?是一个占位符，代表当前位置存在一个外界的变量数据
        String sql = "select u.*, o.*, o.id id_info, o.del del_info from users u inner join userinfos o on u.id = o.u_id where u.username=?";
        List<Object> params = new ArrayList<>();
        params.add(req.getParameter("username"));
        try {
            ResultSet resultSet = (ResultSet) ExecUtil.execute(sql, params, req, false);
            //4.提取结果集中的数据
            while (resultSet.next()) {
                EntityFactory entityFactory = new EntityFactory(Users.class);
                Users users = (Users) entityFactory.startCombine(resultSet);
                this.usersList.add(users);
            }
            resp.getWriter().write(JSON.toJSONString(this.usersList));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //完成转账操作
        if(req.getServletPath().equals("/zhuanzhang")){
            //查询指定账户的余额
            String sql = "select money from users where username=?";
            List<Object> params = new ArrayList<>();
            params.add(req.getParameter("from"));
            ResultSet resultSet = (ResultSet) ExecUtil.execute(sql, params, req, false);
            Double totalMoney = 0.0;
            try {
                while (resultSet.next()){
                    totalMoney = resultSet.getDouble("money");
                }
                if(totalMoney < Double.parseDouble(req.getParameter("money"))){
                    resp.getWriter().write("余额不足");
                }else{
                    sql = "update users set money=money-? where username=?";
                    params.clear();
                    params.add(Double.parseDouble(req.getParameter("money")));
                    params.add(req.getParameter("from"));
                    ExecUtil.execute(sql, params, req, true);

                    Integer num = 100/0;

                    //执行增加操作
                    params.set(0, -Double.parseDouble(req.getParameter("money")));
                    params.set(1, req.getParameter("to"));
                    ExecUtil.execute(sql, params, req, true);
                    ExecUtil.commit();
                    resp.getWriter().write("success");
                }
            }catch (Exception e){
                ExecUtil.rollback();
            }
        }
    }
}
